//
//  CommutationSwitchDataTableViewController.swift
//  AlterationSwitch
//
//  Created by solarke_apple_mini_1 on 2018/10/25.
//  Copyright © 2018年 石家庄科林电气股份有限公司. All rights reserved.
//

import UIKit

class CommutationSwitchDataTableViewController: BaseTableViewController {
    // MARK: - IBOutlet
    @IBOutlet weak var measureDotTextField: UITextField!
    @IBOutlet var labels:[UILabel]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        measureDotTextField.text = "1"
        self.tableView.tableFooterView = UIView(frame: .zero)
        notificationCenter.addObserver(self, selector: #selector(dealWithCommutationSwitchData(sender:)), name: CommutationSwitchDataNotification, object: nil)
    }
    
    @IBAction func backViewTap() {
        view.endEditing(true)
    }
    
    @IBAction func queryButtonTap() {
        if viery() {
            let measureDot = measureDotTextField.text
            let mpNo:Int32 = Int32(measureDot!)!
            let termaddr:UnsafeMutablePointer<UInt8> = getTermaddr()
            let askOneData_fn181_send = ocFile().askOneData_fn181_send(termaddr, a3: a3, mpNo: mpNo)
            var arr = [UInt8](repeating: 0, count: 37)
            //将dst中的所有元素拷贝到arr中
            for i in 0..<37 {
                arr[i] = UInt8(askOneData_fn181_send[i])
            }
            sendMessageBytes(msg: arr)
        }
    }
    
    @objc func dealWithCommutationSwitchData(sender: NSNotification) {
        let info = sender.userInfo as! [String: AnyObject]
        let data = info["data"] as AnyObject
        let time = data.object(forKey: "time") as! String
        labels[0].text = time
        
        var vInt = data.object(forKey: "curVolA") as! Double
        labels[1].text = String(vInt) + "V"
        vInt = data.object(forKey: "curVolB") as! Double
        labels[2].text = String(vInt) + "V"
        vInt = data.object(forKey: "curVolC") as! Double
        labels[3].text = String(vInt) + "V"
        vInt = data.object(forKey: "curLoadVol") as! Double
        labels[4].text = String(vInt) + "V"
        vInt = data.object(forKey: "curLoadCurrent") as! Double
        labels[5].text = String(vInt) + "A"
        vInt = data.object(forKey: "curLoadPowerFactor") as! Double
        labels[6].text = String(vInt)
        vInt = data.object(forKey: "curLoadLekageCurrent") as! Double
        labels[7].text = String(vInt) + "A"
        vInt = data.object(forKey: "curVolUnbalance") as! Double
        labels[8].text = String(vInt) + "%"
        let curPhase = data.object(forKey: "curPhase") as! Int
        switch curPhase {
        case 1:
            labels[9].text = "A相"
        case 2:
            labels[9].text = "B相"
        case 3:
            labels[9].text = "C相"
        default:
            labels[9].text = "未知"
        }
    }
    
    func viery() -> Bool {
        let measureDot = measureDotTextField.text
        if (measureDot?.isEmpty)! {
            showToast("请输入测量点号!")
            return false
        }
        return true
    }
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 12
    }
}
